package demo.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.hibernate.validator.constraints.Range;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/data")
public class DataBindController {

	
	
	@RequestMapping("/getIntFromPath/{id}")
	public Integer getIntFromPath(@PathVariable int id) {
		return id;
	}
	
	@RequestMapping("/getIntegerFromPath/{id}")
	public Integer getIntegerFromPath(@PathVariable Integer id) {
		return id;
	}
	
	@RequestMapping("/getInt")
	public Integer getInt(int id) {
		// id=xxx
		return id;
	}
	
	@RequestMapping("/getInteger")
	public Integer getInteger(Integer id) {
		// id 参数可以不存在
		return id;
	}
	
	@RequestMapping("/getInteger1")
	public Integer getInteger1(@RequestParam(required=false) Integer id) {
		return id;
	}
	
	@RequestMapping("/getInteger2")
	public Integer getInteger2(@RequestParam("id_") Integer id) {
		return id;
	}
	
	@RequestMapping("/getInteger3")
	public Integer getInteger3(@RequestParam(name="id_",defaultValue = "999") Integer id) {
		return id;
	}
	
	@RequestMapping("/getNameArray")
	public String[] getNameArray(String[]name){
		return name;
	}
	
	@RequestMapping("/getNameList")
	public List<String> getNameList(@RequestParam("name") List<String> name){
		// ?name=cat&name=dog
		// 不传参数 报错  
		// org.springframework.web.bind.MissingServletRequestParameterException: Required List parameter 'name' is not present
		return name;
	}
	
	@RequestMapping("/getUser")
	public User getUser(User user){
		// ?id=1&name=cat&dept.id=2&dept.name=dept1
		// set 方法必须
		return user;
	}
	
	@RequestMapping("/getUserAndValid")
	public Map<String,Object> getUserAndValid(@Valid User user,BindingResult bindingResult){
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("user", user);
		
		if(bindingResult!=null && bindingResult.hasErrors()){
			map.put("errors", bindingResult.getAllErrors());
		}
		
		// dept 校验没生效 ！！！

		return map;
	}
	
	@RequestMapping("/getUserFromJson")
	public User getUserFromJson(@RequestBody User user){
		// post 
		// 请求头设置 Content-Type application/json
		// 请求体设置  raw 格式
		// {"id":1,"name":"cat","dept":{"id":2,"name":"dept1"}}
		return user;
	}
	
	
	public static class User{
		
		@NotNull(message = "id为空")
		@Min(value = 0, message = "ID 小于 0")
	    @Max(value = 1000, message = "id 大于 1000")
		private Long id;
		
		@NotBlank(message = "name为空")
		@Size(min=1,max=10,message="name 长度错误 ")
		private String name;
		
		
		private Dept dept;

		public Long getId() {
			return id;
		}

		public void setId(Long id) {
			this.id = id;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public Dept getDept() {
			return dept;
		}

		public void setDept(Dept dept) {
			this.dept = dept;
		}
			
	}
	
	public static class Dept{
		
		@Range(min=1,max=100,message="dept.id 1 ~ 100")
		private Long id;
		
		@Size(min=1,max=10,message="dept name 长度错误 ")
		private String name;
		public Long getId() {
			return id;
		}
		public void setId(Long id) {
			this.id = id;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
			
	}
	

}